﻿using CommHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tea;

namespace NX5WinServies.SDKs.Ali
{
    public class Sms
    {
        public static AlibabaCloud.SDK.Dysmsapi20170525.Client CreateClient()
        {
            // 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378671.html。
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 必填，您的 AccessKey ID
                AccessKeyId = ConfigManager.GetAppSettings("AccessKeyId"),
                // 必填，您的 AccessKey Secret
                AccessKeySecret = ConfigManager.GetAppSettings("AccessKeySecret"),
            };
            // 访问的域名
            config.Endpoint = "dysmsapi.aliyuncs.com";
            return new AlibabaCloud.SDK.Dysmsapi20170525.Client(config);
        }

        public void SendSms(String SignName,String TemplateCode,String PhoneNumbers,String TemplateParam)
        {
            AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient();
            //AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
            //{
            //    SignName = "重庆中升优学智能科技",
            //    TemplateCode = "SMS_493220130",
            //    PhoneNumbers = "18623327700",
            //    TemplateParam = "{\"teacher\":\"王老师\",\"date\":\"2025-8-25 18:00:00\"}",
            //};
            AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
            {
                SignName = SignName,
                TemplateCode = TemplateCode,
                PhoneNumbers = PhoneNumbers,
                TemplateParam = TemplateParam,
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            try
            {
                // 复制代码运行请自行打印 API 的返回值
                client.SendSmsWithOptions(sendSmsRequest, runtime);
            }
            catch (TeaException error)
            {
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
            }
        }
    }
}